@REM Copyright © Huawei Technologies Co., Ltd. 2021-2022. All rights reserved.
@echo off

@REM Get WDK Version
@set WDK_VER=
@call :GetWDKViersion
@if "%WDK_VER%"=="" exit /B 1

@cd /d "%BASE_DIR%kvm-guest-drivers-windows"

@set VIRTIO_GIT_REV=
for /F %%i in ('%GIT_PATH%\git.exe rev-parse --short HEAD') do (
    @set VIRTIO_GIT_REV=%%i
)

@set GIT-DATE=
for /F %%i in ('%GIT_PATH%\git.exe log -n1 --date^=format:"%%Y-%%m-%%d-%%H-%%M-%%S" --pretty^=format:"%%ad" %VIRTIO_GIT_REV%') do (
    @set GIT-DATE=%%i
)

@REM build
msbuild -maxCpuCount virtio-win.sln /t:Rebuild /p:Configuration="Win10 Release" /P:Platform=ARM64;windowstargetplatformVersion=10.0.22000.0;GitRev=%HAX_GIT_REV% -fileLoggerParameters:Verbosity=diag;LogFile=%cd%\Build_Win10_ARM64.log
xcopy /E /C /Y %cd%\NetKVM\Install\Win10\ARM64\ %BASE_DIR%install\Virtio\NetKVM\win11\ARM64\
xcopy /E /C /Y %cd%\NetKVM\Install\Win10\ARM64\netkvm.pdb %BASE_DIR%install\pdb\
xcopy /E /C /Y %cd%\NetKVM\Install\Win10\ARM64\netkvmco.pdb %BASE_DIR%install\pdb\

xcopy /E /C /Y %cd%\ivshmem\Install\Win10\ARM64\ %BASE_DIR%install\Virtio\ivshmem\win11\ARM64\
xcopy /E /C /Y %cd%\ivshmem\Install\Win10\ARM64\ivshmem.pdb %BASE_DIR%install\pdb\

xcopy /E /C /Y %cd%\Balloon\Install\Win10\ARM64\ %BASE_DIR%install\Virtio\Balloon\win11\ARM64\
xcopy /E /C /Y %cd%\Balloon\Install\Win10\ARM64\balloon.pdb %BASE_DIR%install\pdb\
xcopy /E /C /Y %cd%\Balloon\Install\Win10\ARM64\blnsvr.pdb %BASE_DIR%install\pdb\

xcopy /E /C /Y %cd%\pvpanic\Install\Win10\ARM64\ %BASE_DIR%install\Virtio\pvpanic\win11\ARM64\
xcopy /E /C /Y %cd%\pvpanic\Install\Win10\ARM64\pvpanic.pdb %BASE_DIR%install\pdb\

xcopy /E /C /Y %cd%\viogpu\Install\Win10\ARM64\ %BASE_DIR%install\Virtio\viogpu\win11\ARM64\
xcopy /E /C /Y %cd%\viogpu\Install\Win10\ARM64\viogpudo.pdb %BASE_DIR%install\pdb\
xcopy /E /C /Y %cd%\viogpu\Install\Win10\ARM64\vmgtsrv.pdb %BASE_DIR%install\pdb\

xcopy /E /C /Y %cd%\vioserial\Install\Win10\ARM64\ %BASE_DIR%install\Virtio\vioserial\win11\ARM64\
xcopy /E /C /Y %cd%\vioserial\Install\Win10\ARM64\vioser.pdb %BASE_DIR%install\pdb\

xcopy /E /C /Y %cd%\viostor\Install\Win10\ARM64\ %BASE_DIR%install\Virtio\viostor\win11\ARM64\
xcopy /E /C /Y %cd%\viostor\Install\Win10\ARM64\viostor.pdb %BASE_DIR%install\pdb\

xcopy /E /C /Y %cd%\viofs\Install\Win10\ARM64\ %BASE_DIR%install\Virtio\viofs\win11\ARM64\
xcopy /E /C /Y %cd%\viofs\Install\Win10\ARM64\viofs.pdb %BASE_DIR%install\pdb\

xcopy /E /C /Y %cd%\vioinput\Install\Win10\ARM64\ %BASE_DIR%install\Virtio\vioinput\win11\ARM64\
xcopy /E /C /Y %cd%\vioinput\Install\Win10\ARM64\vioinput.pdb %BASE_DIR%install\pdb\
xcopy /E /C /Y %cd%\vioinput\Install\Win10\ARM64\viohidkmdf.pdb %BASE_DIR%install\pdb\

xcopy /Y %cd%\Build_Win10_ARM64.log %BASE_DIR%\install\Virtio\

del /f /s /q %BASE_DIR%install\Virtio\NetKVM\win11\ARM64\netkvm.pdb
del /f /s /q %BASE_DIR%install\Virtio\NetKVM\win11\ARM64\netkvmco.pdb
del /f /s /q %BASE_DIR%install\Virtio\NetKVM\win11\ARM64\readme.doc
del /f /s /q %BASE_DIR%install\Virtio\ivshmem\win11\ARM64\ivshmem.pdb
del /f /s /q %BASE_DIR%install\Virtio\ivshmem\win11\ARM64\ivshmem-test.exe
del /f /s /q %BASE_DIR%install\Virtio\ivshmem\win11\ARM64\ivshmem-test.pdb
del /f /s /q %BASE_DIR%install\Virtio\Balloon\win11\ARM64\balloon.pdb
del /f /s /q %BASE_DIR%install\Virtio\Balloon\win11\ARM64\blnsvr.pdb
del /f /s /q %BASE_DIR%install\Virtio\pvpanic\win11\ARM64\pvpanic.cat
del /f /s /q %BASE_DIR%install\Virtio\pvpanic\win11\ARM64\pvpanic.inf
del /f /s /q %BASE_DIR%install\Virtio\pvpanic\win11\ARM64\pvpanic.pdb

del /f /s /q %BASE_DIR%install\Virtio\viogpu\win11\ARM64\viogpuap.exe
del /f /s /q %BASE_DIR%install\Virtio\viogpu\win11\ARM64\viogpuap.pdb
del /f /s /q %BASE_DIR%install\Virtio\viogpu\win11\ARM64\viogpudo.pdb
del /f /s /q %BASE_DIR%install\Virtio\viogpu\win11\ARM64\vmgtsrv.pdb

del /f /s /q %BASE_DIR%install\Virtio\vioserial\win11\ARM64\vioser.pdb
del /f /s /q %BASE_DIR%install\Virtio\vioserial\win11\ARM64\vioser-test.exe
del /f /s /q %BASE_DIR%install\Virtio\vioserial\win11\ARM64\vioser-test.pdb


del /f /s /q %BASE_DIR%install\Virtio\viostor\win11\ARM64\viostor.pdb
del /f /s /q %BASE_DIR%install\Virtio\viofs\win11\ARM64\viofs.pdb

del /f /s /q %BASE_DIR%install\Virtio\vioinput\win11\ARM64\vioinput.pdb
del /f /s /q %BASE_DIR%install\Virtio\vioinput\win11\ARM64\viohidkmdf.pdb

@cd /d "%BASE_DIR%kvm-guest-drivers-windows\vmgt"
msbuild -maxCpuCount vmgt.sln /t:Rebuild /p:Configuration="Release" /P:Platform=ARM64;windowstargetplatformVersion=10.0.22000.0
@cd /d "%BASE_DIR%kvm-guest-drivers-windows"
xcopy /E /C /Y %cd%\vmgt\ARM64\Release\vmgtsharemgr.exe %BASE_DIR%install\Virtio\viogpu\win11\ARM64\
xcopy /E /C /Y %cd%\vmgt\ARM64\Release\vmgtsharemgr.pdb %BASE_DIR%install\pdb\

@cd /d "%BASE_DIR%kvm-guest-drivers-windows\hwfs\hwfs_np"
msbuild /p:Platform=win32 /p:Configuration="release";windowstargetplatformVersion=10.0.22000.0
@cd /d "%BASE_DIR%kvm-guest-drivers-windows\hwfs"
msbuild /p:Platform=arm64 /p:Configuration="release";windowstargetplatformVersion=10.0.22000.0
@cd /d "%BASE_DIR%kvm-guest-drivers-windows"
xcopy /E /C /Y %cd%\hwfs\ARM64\Release\*.* %BASE_DIR%install\Virtio\hwfs\win11\ARM64\
del /f /s /q %BASE_DIR%install\Virtio\hwfs\win11\ARM64\kstl.lib
xcopy /E /C /Y %cd%\hwfs\ARM64\Release\hwfs.pdb %BASE_DIR%install\pdb

@cd /d "%BASE_DIR%kvm-guest-drivers-windows"

echo Time:%GIT-DATE%  Commit:%VIRTIO_GIT_REV%> %cd%\Virtio_Version.log
xcopy /Y %cd%\Virtio_Version.log %BASE_DIR%\install\Virtio\

@call %cd%\clean.bat
if exist Virtio_Version.log del Virtio_Version.log
if exist Build_Win10_ARM64.log del Build_Win10_ARM64.log
@exit /B 0

@REM -----------------------------------------------------------------------Functions been used.

:GetWDKViersion
for /f "skip=2 tokens=*" %%n in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows Kits\Installed Roots" /v "WdkBinRootVersioned"') DO (
    @set WDK_VER=%%n
)
@set WDK_VER=%WDK_VER:*\bin\=%%
@set WDK_VER=%WDK_VER:\=%
@if "%WDK_VER%"=="" exit /B 1
@exit /B 0